﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;
using System.Windows.Media.Imaging;
using ComicIsland.Common.Entities;
using ComicIsland.Common.StorageHelper;

/**
 * 创建时间：2014/12/17 14:51:15
 * 作者：yong.blackcore
 * Email：itblackhole@outlook.com
 * QQ：50404503
 */
namespace ComicIsland.Common.Converter
{
    public class DownloadStatusConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            try
            {
                //string comicAndPartId = string.Empty;
                //if (value != null)
                //{
                //    comicAndPartId = value.ToString();
                //}
                //string showType = "number";
                //if (parameter != null)
                //{
                //    showType = parameter.ToString();
                //}

                //if (!string.IsNullOrWhiteSpace(comicAndPartId))
                //{
                //    var cpIds = comicAndPartId.Split('#');
                //    if (cpIds != null && cpIds.Length == 2)
                //    {
                //        int comicId = 0, partId = 0;
                //        int.TryParse(cpIds[0], out comicId);
                //        int.TryParse(cpIds[1], out partId);
                //        string key = string.Format(KEYS.CACHEDOWNLOAD_CHAPTER_IMAGE_QUEUE, comicId, partId);
                //        //
                //        ObservableCollection<DownloadComicChapterImage> items = null;
                //        int result = 0;
                //        if (!IsolatedStorageHelper.Instance.FileExists(key))
                //        {
                //            result = 0;
                //        }
                //        else
                //        {
                //            string localCacheData = IsolatedStorageHelper.Instance.ReadText(key);
                //            if (!string.IsNullOrWhiteSpace(localCacheData))
                //            {
                //                items = JSONHelper.Deserialize<ObservableCollection<DownloadComicChapterImage>>(localCacheData);
                //            }
                //        }
                //        //                        LogHelper.LogInfo(string.Concat(showType, "\t", key), this.GetType(), MethodBase.GetCurrentMethod());
                //        switch (showType)
                //        {
                //            case "number":
                //                if (items != null && items.Count > 0)
                //                {
                //                    result = items.Where<DownloadComicChapterImage>(p => p.DownloadStatus == (int)DownloadComicChapterStatus.Completed).Count();
                //                }                                
                //                LogHelper.LogInfo(string.Concat(showType, "\t", result, "\t", key), this.GetType(), MethodBase.GetCurrentMethod());
                //                return result;
                //            case "status":
                //                if (items != null && items.Count > 0)
                //                {
                //                    int totalCompleted = items.Where<DownloadComicChapterImage>(p => p.DownloadStatus == (int)DownloadComicChapterStatus.Completed).Count();
                //                    if (totalCompleted == items.Count)
                //                    {
                //                        return "已完成";
                //                    }
                //                    else
                //                        return "下载中";
                //                }
                //                break;
                //            case "image":
                //                if (items != null && items.Count > 0)
                //                {
                //                    int totalCompleted = items.Where<DownloadComicChapterImage>(p => p.DownloadStatus == (int)DownloadComicChapterStatus.Completed).Count();
                //                    //LogHelper.LogInfo(string.Concat(showType, "\t", totalCompleted, "\t", key), this.GetType(), MethodBase.GetCurrentMethod());
                //                    if (totalCompleted == items.Count)
                //                    {
                //                        //BitmapImage bitImage = new BitmapImage();
                //                        //bitImage.UriSource = new Uri(KEYS.RESOURCE_ASSEMBLYNAME + "/Images/Icons/play.png", UriKind.Relative);
                //                        //return bitImage;
                //                        return "/Images/Icons/play.png";
                //                    }
                //                }
                //                return "/Images/Icons/download.png";                                
                //            case "progress":
                //                if (items != null && items.Count > 0)
                //                {
                //                    int totalCompleted = items.Where<DownloadComicChapterImage>(p => p.DownloadStatus == (int)DownloadComicChapterStatus.Completed).Count();
                //                    return (totalCompleted / items.Count) * 100.00;
                //                }
                //                return 0;

                //            default:
                //                break;
                //        }

                //    }
                //}

                return "_";
            }
            catch (Exception err)
            {
                LogHelper.LogError(err.Message, this.GetType(), MethodBase.GetCurrentMethod());
                return "";
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return null;
        }
    }
}
